/**
 * Ingenius-Group S.A.S
 *
 * Project: SAU
 * File: ListRolesView.java
 * Date: 10/03/2011
 */

package com.ingenius.sau.client.view;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.ingenius.sau.client.presenter.ListAcademicProgramsPresenter.ListAcademicProgramsDisplay;
import com.ingenius.sau.client.view.widgets.ListBox;
import com.ingenius.sau.shared.model.programs.AcademicProgram;
import com.ingenius.sau.shared.util.filter.Comparison;
import com.ingenius.sau.shared.util.filter.Filter;
import com.ingenius.sau.shared.util.filter.NumberFilter;
import com.ingenius.sau.shared.util.filter.StringFilter;

/**
 * Class <strong>ListRolesView</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
public class ListAcademicProgramsView extends AbstractListView<AcademicProgram>
		implements ListAcademicProgramsDisplay {

	// Singleton
	private static ListAcademicProgramsView instance;

	// UI Widgets
	private TextField<String> filterName = new TextField<String>();
	private TextField<String> filterCode = new TextField<String>();
	private TextField<String> filterTitleGranted = new TextField<String>();
	private TextField<String> filterDurationFrom = new TextField<String>();
	private TextField<String> filterDurationTo = new TextField<String>();
	private ListBox filterModality = new ListBox();

	private ListAcademicProgramsView() {
	}

	public static ListAcademicProgramsView getInstance() {
		if (instance == null) {
			instance = new ListAcademicProgramsView();
		}
		return instance;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.view.AbstractListView#onRender(com.google.gwt
	 * .user.client.Element, int)
	 */
	@Override
	protected void onRender(Element parent, int index) {
		super.onRender(parent, index);
		// TODO i18n
		gridContainer.setHeading("Academic Programs");
		gridContainer.setWidth(845);
		configureHeader();
		configureSubmenu();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.view.AbstractListView#createColumnModel()
	 */
	@Override
	protected ColumnModel createColumnModel() {
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		// TODO i18n 2º columna
		ColumnConfig column = new ColumnConfig("code", "Code", 50);
		columns.add(column);

		column = new ColumnConfig("name", "Name", 100);
		columns.add(column);

		column = new ColumnConfig("mission", "Mission", 100);
		columns.add(column);

		column = new ColumnConfig("vision", "Vision", 100);
		columns.add(column);

		column = new ColumnConfig("titleGranted", "Title granted", 100);
		columns.add(column);

		column = new ColumnConfig("duration", "Duration", 40);
		column.setColumnStyleName("columnCenter");
		column.setStyle("text-align: center;");
		columns.add(column);

		column = new ColumnConfig("modality", "Modality", 70);
		column.setColumnStyleName("columnCenter");
		column.setStyle("text-align: center;");
		columns.add(column);

		return new ColumnModel(columns);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.view.AbstractListView#createAdvancedSearch()
	 */
	@Override
	protected Widget createAdvancedSearch() {
		// TODO i18n
		final DisclosurePanel panel = new DisclosurePanel("Advanced search");
		panel.setStyleName("disclosure-panel");
		panel.setAnimationEnabled(false);

		FlexTable table = new FlexTable();
		table.setStyleName("advanced-search");
		table.setCellSpacing(12);
		table.setCellPadding(12);

		// TODO i18n
		table.setHTML(0, 0, "Name");
		table.setWidget(0, 1, filterName);

		table.setHTML(0, 2, "SNIES code");
		table.setWidget(0, 3, filterCode);

		table.setHTML(0, 4, "Title granted");
		table.setWidget(0, 5, filterTitleGranted);

		table.setHTML(1, 0, "Duration (from)");
		table.setWidget(1, 1, filterDurationFrom);

		table.setHTML(1, 2, "Duration (to)");
		table.setWidget(1, 3, filterDurationTo);

		table.setHTML(1, 4, "Modality");
		table.setWidget(1, 5, filterModality);

		HorizontalPanel buttons = new HorizontalPanel();
		buttons.setSpacing(10);
		buttons.setHorizontalAlignment(HasAlignment.ALIGN_CENTER);

		// TODO i18n
		Button searchButton = new Button("Search");
		searchButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				doSearch();
			}
		});
		// TODO i18n
		Button cancelButton = new Button("Cancel");
		cancelButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				doCancel();
				panel.setOpen(false);
			}
		});

		buttons.add(searchButton);
		buttons.add(cancelButton);

		VerticalPanel wrap = new VerticalPanel();
		wrap.add(table);
		wrap.add(buttons);
		wrap.setCellVerticalAlignment(buttons, HasAlignment.ALIGN_BOTTOM);
		wrap.setCellHorizontalAlignment(buttons, HasAlignment.ALIGN_CENTER);
		panel.add(wrap);

		return panel;
	}

	private void doSearch() {
		if (filters == null) {
			filters = new ArrayList<Filter>();
		}
		filters.clear();

		if (filterName.getValue() != null) {
			filters.add(new StringFilter(Comparison.EQUALS, "name", filterName
					.getValue()));
		}
		if (filterCode.getValue() != null) {
			filters.add(new StringFilter(Comparison.EQUALS, "code", filterCode
					.getValue()));
		}
		if (filterTitleGranted.getValue() != null) {
			filters.add(new StringFilter(Comparison.EQUALS, "titleGranted",
					filterTitleGranted.getValue()));
		}
		if (filterModality.getSimpleValue() != null
				&& !filterModality.getSimpleValue().equals("---")) {

			filters.add(new StringFilter(Comparison.EQUALS, "modality",
					filterModality.getSimpleValue()));
		}
		if (filterDurationFrom.getValue() != null) {
			try {
				Integer value = Integer.parseInt(filterDurationFrom.getValue());
				filters.add(new NumberFilter(Comparison.GREATER_THAN_OR_EQUAL,
						"duration", value));
			} catch (NumberFormatException e) {

			}
		}
		if (filterDurationTo.getValue() != null) {
			try {
				Integer value = Integer.parseInt(filterDurationTo.getValue());
				filters.add(new NumberFilter(Comparison.LESS_THAN_OR_EQUAL,
						"duration", value));
			} catch (NumberFormatException e) {

			}
		}
		super.reload();
	}

	private void doCancel() {
		filterName.setValue("");
		filterTitleGranted.setValue("");

		if (filters != null) {
			filters.clear();
			super.reload();
		}
	}

	protected void configureHeader() {
		// TDOD i18n
		String moduleName = "Academic Programs";
		String html = "<span class='module-header'>" + moduleName + "</span>";
		headerContainer.add(new Html(html));
	}

	protected void configureSubmenu() {
		submenuContainer.setWidth(10);
	}

	public void setModalities(List<String> modalities) {
		filterModality.setEditable(false);
		filterModality.add("---");
		for (String modality : modalities) {
			// TODO i18n 2º column
			filterModality.add(modality, modality);
		}
	}

}
